package es.andresimon.portfolio.domain;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Configurable
public class Picture {

    private Long pictureId;

    private Integer weight;

    @NotNull
    @Size(min = 1, max = 100)
    private String description;

    @NotNull
    @Size(min = 1, max = 100)
    private String name;

    @Size(min = 1, max = 100)
    private String url;

	public static Query findPicturesByPictureId(Long pictureId) {
        if (pictureId == null) throw new IllegalArgumentException("The pictureId argument is required");
        EntityManager em = Picture.entityManager();
        Query q = em.createQuery("SELECT Picture FROM Picture AS picture WHERE picture.pictureId = :pictureId");
        q.setParameter("pictureId", pictureId);
        return q;
    }

	public Long getPictureId() {
        return this.pictureId;
    }

	public void setPictureId(Long pictureId) {
        this.pictureId = pictureId;
    }

	public Integer getWeight() {
        return this.weight;
    }

	public void setWeight(Integer weight) {
        this.weight = weight;
    }

	public String getDescription() {
        return this.description;
    }

	public void setDescription(String description) {
        this.description = description;
    }

	public String getName() {
        return this.name;
    }

	public void setName(String name) {
        this.name = name;
    }

	public String getUrl() {
        return this.url;
    }

	public void setUrl(String url) {
        this.url = url;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	@Transactional(propagation = Propagation.REQUIRES_NEW)
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Picture attached = this.entityManager.find(this.getClass(), this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public Picture merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Picture merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public static final EntityManager entityManager() {
        EntityManager em = new Picture().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	@Transactional
    public static long countPictures() {
        return ((Number) entityManager().createQuery("select count(o) from Picture o").getSingleResult()).longValue();
    }

	@SuppressWarnings("unchecked")
    @Transactional
    public static List<Picture> findAllPictures() {
        return entityManager().createQuery("select o from Picture o").getResultList();
    }

	@Transactional
    public static Picture findPicture(Long id) {
        if (id == null) return null;
        return entityManager().find(Picture.class, id);
    }

	@SuppressWarnings("unchecked")
    @Transactional
    public static List<Picture> findPictureEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("select o from Picture o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Id: ").append(getId()).append(", ");
        sb.append("Version: ").append(getVersion()).append(", ");
        sb.append("PictureId: ").append(getPictureId()).append(", ");
        sb.append("Weight: ").append(getWeight()).append(", ");
        sb.append("Description: ").append(getDescription()).append(", ");
        sb.append("Name: ").append(getName()).append(", ");
        sb.append("Url: ").append(getUrl());
        return sb.toString();
    }
}
